using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class CategoryTree : AdminBaseUserControl
    {
        public event EventHandler TreeNodeClick;
        public event EventHandler EvenLogMenu;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (EvenLogMenu != null)
                    EvenLogMenu(this, new EventArgs());  
               //DataBind();
              
            }
            
            string target = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrEmpty(target))
            {
                if (target.Equals(this.tvCategory.UniqueID))
                {
                    if (ViewState["previousArg"] != null)
                    {
                        string previousArg = (string)ViewState["previousArg"];
                        if (previousArg.Equals(Request.Form["__EVENTARGUMENT"]))
                        {
                            try{
                                if (TreeNodeClick != null && tvCategory.SelectedNode.Depth == 1)
                                {
                                    this.SelectedValue = tvCategory.SelectedValue; 

                                    TreeNodeClick(this, e);
                                }  
                            }
                            catch
                            {
                            }
                        }
                    }
                    ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
                }
            }

        }

        public  string SelectedValue
        {
            get { return Convert.ToString(ViewState["_ClassID"]); }
            set { ViewState["_ClassID"] = value; }
        }
        
        private  void PopulateBind(IContentCategory category, TreeNode node)
        {
            bool add = true;
            if (category.CategoryList!=null)
            foreach (IContentCategory cat in category.CategoryList)
            {
                for (int i = 0; i < cat.ExtPropertyList.Count; i++)
                {
                    if ((Convert.ToString(cat.ExtPropertyList[i].Name) == "TypeClass") && (Convert.ToString(cat.ExtPropertyList[i].Value) == "Forum"))
                    {
                        add = false;
                    }
                }

                if (add == true)
                {
                    TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                    node.ChildNodes.Add(subNode);
                    PopulateBind(cat, subNode);
                }
                else
                    add = true;
            }

        }  
        
        public override void DataBind()
        {
            #region get cat
            long classID=0;
            IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.ContentCategory);
            foreach (ICmsClass clazz in cList)
            {
                for (int i = 0; i < clazz.PropertyList.Count; i++)
                    if ((Convert.ToString(clazz.PropertyList[i].Name) == "Type") && (Convert.ToString(clazz.PropertyList[i].Value) == "Media"))
                    {
                        classID = clazz.ID;
                        break;
                    }
            }

            #endregion

            tvCategory.Nodes.Clear();
            IContentCategory root=new Category();
            IContentCategory rootCat = CategoryController.GetCategoryTree();
            foreach (Category c in rootCat.CategoryList)
            {
                if (c.ClassID != classID)
                    root.CategoryList.Add(c);
            }

            TreeNode nodeRoot = new TreeNode("Categories", "0");
            nodeRoot.NavigateUrl = "javascript:void(0)";  
            PopulateBind(root, nodeRoot);
            tvCategory.Nodes.Add(nodeRoot);
            tvCategory.ExpandAll();   
        }

       
        public void BindMedia()
        {
            if (Session[SessionVariable.Clazz.CurrentID] != null)
            {
                tvCategory.Nodes.Clear();
                IContentCategory root = CategoryController.GetCategoryTreeByClassID(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
                TreeNode nodeRoot = new TreeNode("Photos", "0");
                nodeRoot.NavigateUrl = "javascript:void(0)";
                if (root.CategoryList != null)
                    foreach (IContentCategory cat in root.CategoryList)
                    {
                        TreeNode subNode = new TreeNode(cat.Name, cat.ID.ToString());
                        nodeRoot.ChildNodes.Add(subNode);
                    }

                tvCategory.Nodes.Add(nodeRoot);
                tvCategory.ExpandAll();
            }
        }

        public void BindRefesh(object sender, EventArgs e)
        {
            //DataBind();
            BindMedia();
        }

        public void BindCategory(object sender, EventArgs e)
        {
            DataBind();
        }

        protected void tvCategory_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TreeNodeClick != null)            
            {
                this.SelectedValue = tvCategory.SelectedValue; 
                
                TreeNodeClick(this, e);
            }
            try
            {
                ViewState["previousArg"] = Request.Form["__EVENTARGUMENT"];
            }
            catch (Exception ex)
            {
                Logger.Error("Unknown error", ex);
            }
            
        }

        
        public void DoDeleteCategory(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectedValue.ToString()))
            {
                long catID = Convert.ToInt64(this.SelectedValue);

                
                    IContentCategory category = CategoryController.GetCategoryNode(catID);
                    foreach (IContentCategory cat in category.CategoryList)
                    {
                        CategoryController.DeleteCategory(cat.ID);
                        //delete image
                        try
                        {
                            string path = Path.Combine(DataRootPhysicalPath, "Photos/" + cat.ID + ".jpg");
                            FileInfo file = new FileInfo(path);
                            file.Delete();
                        }
                        catch { }
                    }

                    CategoryController.DeleteCategory(catID);
                    try
                    {
                        string path = Path.Combine(DataRootPhysicalPath, "Photos/" + catID + ".jpg");
                        FileInfo file = new FileInfo(path);
                        file.Delete();
                    }
                    catch { }

              
                BindMedia();
                DataBind();
               
            }
        }
    }

}